package aps

import com.thoughtworks.xstream.XStream
import glory.xxd.ConsumerUtil
import glory.xxd.ProducerUtil
import glory.xxd.Utils

/**
 * 消息数据解析
 */
class CommentService  {


    def senQueueName = "jms.queue.APS.EAPADMIN"
    def replyQueueName = "jms.queue.APS.EAPADMIN.REPLY"
    public final static Map map = new HashMap();

    /**
     * 获取发送topic接收的queue消息 返回json
     * @param logUUID
     * @param header  消息头
     * @param body   消息体
     * @return   body内的内容 json格式
     */
    def getJsonByObjectForTopic2Queue(logUUID, header, body) {
        XStream xstream = new XStream();
        xstream.alias("Request", RequestInstance.class)
        xstream.alias("Body", body.getClass())
        def requestInstance = new RequestInstance()
        requestInstance.setBody(body)
        requestInstance.setHeader(header)
        def xml = xstream.toXML(requestInstance)

        def msgState = ProducerUtil.sendTopicMsg(logUUID, "jms.topic.APS.Client2EAP", xml)

        def json
        if ("1".equals(msgState)) {
            def xmlReturn = ConsumerUtil.getQueueMsg(logUUID, "jms.queue.EAP.REPLY.Client")
            if (xmlReturn) {
                xmlReturn = xmlReturn[0]
                json = Utils.getJsonByXML(xmlReturn, "Response/.Body")
            }
        }
        return json
    }

    /**
     * 获取发送queue接收的queue消息 返回json
     * @param logUUID
     * @param header  消息头
     * @param body   消息体
     * @return   body内的内容 json格式
     */
    def getJsonByObjectForQueue2Queue(logUUID, header, body) {
        XStream xstream = new XStream();
        xstream.alias("Request", RequestInstance.class)
        xstream.alias("Body", body.getClass())
        def requestInstance = new RequestInstance()
        requestInstance.setBody(body)
        requestInstance.setHeader(header)
        def xml = xstream.toXML(requestInstance)

        def msgState = ProducerUtil.sendQueueMsg(logUUID, senQueueName, xml)

        def json
        if ("1".equals(msgState)) {
            def xmlReturn = ConsumerUtil.getQueueMsg(logUUID, replyQueueName)
            if (xmlReturn) {
                xmlReturn = xmlReturn[0]
                json = Utils.getJsonByXML(xmlReturn, "Response/.Body")
            }
        }
        return json
    }

    /**
     *
     * @param logUUID   messageID
     * @param xml   消息内容xml格式
     * @return  body内的内容 json格式
     */
    def getJsonByObjectForTopic2Queue(logUUID, xml) {
        def msgState = ProducerUtil.sendTopicMsg(logUUID, "jms.topic.APS.Client2EAP", xml)
        def json
        if ("1".equals(msgState)) {
            def xmlReturn = ConsumerUtil.getQueueMsg(logUUID, "jms.queue.EAP.REPLY.Client")
            if (xmlReturn) {
                xmlReturn = xmlReturn[0]
                json = Utils.getJsonByXML(xmlReturn, "Response/.Body")
            }
        }
        return json
    }

}